import QQMapWX from './qqmap-wx-jssdk.min.js'

let qqmapsdk = new QQMapWX({
	key: '7J2BZ-W5PR3-EDH3E-YC2LT-OFPG3-O2FJY'
})
const initAddr = () => {
	let addr = uni.getStorageSync('address')
	let newAddr = {}
	newAddr = addr ? addr : {
		lat: 37.870472,
		lng: 112.548877,
		city: '成都',
		province: '510000'	//省
	}
	uni.setStorageSync('address', newAddr)
	return newAddr
}
const location = (isModal = true) => {
	return new Promise((resolve, reject) => {
		uni.getLocation({
			type: 'gcj02',
			geocode: true,
			success: res => {
				qqmapsdk.reverseGeocoder({
					location: {
						latitude: res.latitude,
						longitude: res.longitude
					},
					success: function(res2) {
						let addr = res2.result.ad_info
						uni.setStorageSync('address', {
							lat: res.latitude,
							lng: res.longitude,
							city: addr.city
						})
						resolve({
							lat: res.latitude,
							lng: res.longitude,
							city: addr.city
						})
					},
					fail: function() {
						reject(initAddr())
					}
				});
			},
			fail: () => {
				if(isModal) {
					wx.getSetting({
						success: (res) => {
							if (!res.authSetting['scope.userLocation']) {
								wx.showModal({
									content: '检测到您没打开定位权限，是否去设置打开？',
									success: function(res) {
										if (res.confirm) {
											wx.openSetting()
										}
									},
								});
							} else {
								uni.showToast({
									title: '检测到您没打开定位开关',
									icon: 'none'
								})
							}
						},
					
					})
				}
				
				reject(initAddr())
			}
		})
	})
}
export default location